import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
from model import President, SpeechCategory, SpeechText, Party
import csv, os
import ConfigParser
from datetime import date

import logging
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger( 'Upload' )

def upload_dataset():
    reader = csv.reader(open('data/party.csv'))
    for row in reader:
    	log.debug(str(row))
      	party = Party()
      	party.id = int(row[0])
      	party.name = row[1]
      	party.put()

    reader = csv.reader(open('data/president.csv'))
    for row in reader:
      	log.debug(str(row))
      	president = President()
      	president.id = int(row[0])
      	president.name = row[1]
      	l = row[2].split('/')
      	president.start_date = date(int(l[2]), int(l[0]), int(l[1]))
      	l = row[3].split('/')
      	president.end_date = date(int(l[2]), int(l[0]), int(l[1]))
      	president.terms = int(row[4])
      	result = Party.all().filter('id = ', int(row[5])).get()
      	log.debug('PARTY'+result.name)
      	president.party = result
      	president.put()
	 
	 



#if __name__ == "__main__":                                                                                                                             
#  main()     